package com.project.petLossSystem.controller.common;

import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 处理错误网络代码
 */
@Controller
public class ErrorPageController implements ErrorViewResolver {

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("/pc/error/error_status");
        // 添加状态码
        modelAndView.addObject("status", status.value());
        // 根据状态码找原因
        if(HttpStatus.BAD_REQUEST.equals(status)){
            modelAndView.addObject("reason", "参数错误，请修改参数！");
        }else if(HttpStatus.NOT_FOUND.equals(status)){
            modelAndView.addObject("reason", "资源不存在，请修改url请求！");
        }else if(HttpStatus.INTERNAL_SERVER_ERROR.equals(status)){
            modelAndView.addObject("reason", "服务器错误，请联系工作人员！");
        }else{
            modelAndView.addObject("reason", "未知错误，请联系工作人员！");
        }
        return modelAndView;
    }
}
